package com.yuanfeng.job.controller;


import com.yuanfeng.commoms.exception.BizCodeEnume;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.job.dto.JobDTO;
import org.quartz.SchedulerException;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("job")
public class JobPlatformJobController extends BaseController{



    /**
     * 查询平台所有定时任务
     *
     * @param paraMap
     * @return
     */
    @PostMapping("context/job/getAllJob")
    public ResponseResult queryJob(@RequestBody Map<String, String> paraMap) {
        return jobPlatformService.queryJob(paraMap);
    }

    /**
     * 查询启动中的定时任务
     *
     * @param job
     * @return
     */
    @PostMapping("/monitor/job/list")
    public ResponseResult list(@RequestBody JobDTO job) {
        return jobPlatformService.selectJobAll(job);
    }

    /**
     * 添加 定时任务
     *
     * @param job
     * @return
     */
    @PostMapping("/monitor/job/add")
    public ResponseResult addJob(@RequestBody JobDTO job) {
        if (job.getJobName() == null || "".equals(job.getJobName().trim())) {
            return ResponseResult.fail(BizCodeEnume.NO_SET_JOB_NAME);
        } else if (job.getInvokeTarget() == null || "".equals(job.getInvokeTarget().trim())) {
            return ResponseResult.fail(BizCodeEnume.NO_SET_JOB_INVOKETARGET);
        } else if (job.getMouth() != null && "".equals(job.getMouth().trim())) {
            return ResponseResult.fail(BizCodeEnume.NO_SET_JOB_MOUTH);
        } else if (job.getWeek() != null && "".equals(job.getWeek().trim())) {
            return ResponseResult.fail(BizCodeEnume.NO_SET_JOB_WEEK);
        } else if (job.getDay() != null && "".equals(job.getDay().trim())) {
            return ResponseResult.fail(BizCodeEnume.NO_SET_JOB_DAY);
        } else if (job.getHour() != null && "".equals(job.getHour().trim())) {
            return ResponseResult.fail(BizCodeEnume.NO_SET_JOB_HOUR);
        } else if (job.getMin() != null && "".equals(job.getMin().trim())) {
            return ResponseResult.fail(BizCodeEnume.NO_SET_JOB_MIN);
        } else {
            return jobPlatformService.addJob(job);
        }
    }

    /**
     * 修改定时任务
     *
     * @param job
     * @return
     */
    @PostMapping("/monitor/job/editJobById")
    public ResponseResult editJobById(@RequestBody JobDTO job) throws SchedulerException {
        if (job.getJobId() == null) {
            return ResponseResult.fail(BizCodeEnume.INVALID_PARAMETER);
        } else if (job.getJobName() == null || "".equals(job.getJobName().trim())) {
            return ResponseResult.fail(BizCodeEnume.NO_SET_JOB_NAME);
        } else if (job.getInvokeTarget() == null || "".equals(job.getInvokeTarget().trim())) {
            return ResponseResult.fail(BizCodeEnume.NO_SET_JOB_INVOKETARGET);
        } else if (job.getMouth() != null && "".equals(job.getMouth().trim())) {
            return ResponseResult.fail(BizCodeEnume.NO_SET_JOB_MOUTH);
        } else if (job.getWeek() != null && "".equals(job.getWeek().trim())) {
            return ResponseResult.fail(BizCodeEnume.NO_SET_JOB_WEEK);
        } else if (job.getDay() != null && "".equals(job.getDay().trim())) {
            return ResponseResult.fail(BizCodeEnume.NO_SET_JOB_DAY);
        } else if (job.getHour() != null && "".equals(job.getHour().trim())) {
            return ResponseResult.fail(BizCodeEnume.NO_SET_JOB_HOUR);
        } else if (job.getMin() != null && "".equals(job.getMin().trim())) {
            return ResponseResult.fail(BizCodeEnume.NO_SET_JOB_MIN);
        } else {
            return jobPlatformService.editJobById(job);
        }
    }

    /**
     * 删除定时任务
     *
     * @param job
     * @return
     */
    @PostMapping("/monitor/job/deleteJobById")
    public ResponseResult deleteJobById(@RequestBody JobDTO job) throws Exception {
        return jobPlatformService.deleteJobById(job);
    }

    /**
     * 暂停 / 恢复 定时任务
     *
     * @param job
     * @return
     */
    @PostMapping("/monitor/job/stopJobById")
    public ResponseResult stopJobById(@RequestBody JobDTO job) throws Exception {
        return jobPlatformService.stopJobById(job);
    }

}
